#include "Queue.h"

int init_queue(Queue_t *queue){
    bzero(queue,sizeof(Queue_t));
    return 0;
}


int insert_tail(Queue_t *queue,int netfd){
    Node *node = (Node *)calloc(1,sizeof(Node));
    if(node == NULL){
        printf("node calloc failed in initQueue in insert_tail\n");
        exit(-1);
    }
    node->val = netfd;
    if(queue->size == 0){
        queue->head = node;
        queue->tail = node;
        queue->size++;
        return 0;
    }
    queue->tail->next = node;
    queue->tail = node;
    queue->size++;
    return 0;
}

int delete_head(Queue_t *queue){
    Node *p = queue->head;
    if(queue->size == 1){
        queue->head = NULL;
        queue->tail = NULL;
        queue->size--;
        free(p);
        return 0;
    }
    queue->head = p->next;
    free(p);
    queue->size--;
    return 0;
}

int print_queue(Queue_t *queue){
    if(queue->size == 0){
        printf("the queue is empty");
        return 0;
    }

    Node *p = queue->head;

    while(p!=NULL){
        printf("%3d",p->val);
        p=p->next;
    }
    printf("\n");
    return 0;
}



